﻿using Hangfire;
using LiveShopvnext.Books;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace LiveShopvnext.Hangfire
{
    public class HangfireService : BackgroundService
    {
        private readonly IRepository<Book, Guid> _repository;
        public HangfireService(IRepository<Book, Guid> repository) 
        {
            _repository = repository;
        }

        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            //任务每分钟执行一次
            RecurringJob.AddOrUpdate(() => HangfireJob(), Cron.Hourly());

            return Task.CompletedTask;
        }

        public void HangfireJob()
        {
            var book = new Book() { Name = "自动任务图书", Price = 100, PublishDate = DateTime.Now };
            _repository.InsertAsync(book);

        }
    }
}
